#include <iostream>
#include <algorithm>

using namespace std;

const int N = 20, M = 60;

int n, m;
int w[N][M], way[N];
int f[N][M];

int main() {
    cin >> n >> m;
    
    for (int i = 1; i <= n; ++ i)
        for (int j = 1; j <= m; ++ j)
            scanf("%d", &w[i][j]);
            
    for (int i = 1; i <= n; ++ i)
        for (int j = 0; j <= m; ++ j)
            for (int k = 0; k <= j; ++ k)
                f[i][j] = max(f[i][j], f[i - 1][j - k] + w[i][k]);
    
    cout << f[n][m] << endl;
    int j = m;
    for (int i = n; i; -- i)
        for (int k = 0; k <= j; ++ k)
            if (f[i][j] == f[i - 1][j - k] + w[i][k]) {
                way[i] = k;
                j -= k;
                break;
            }
            
    for (int i = 1; i <= n; ++ i) cout << i << ' ' << way[i] << endl;
    
    return 0;    
}